<template>
  <el-select
    v-model="language"
    :placeholder="$t('msg.navBar.lang')"
    @change="handleSetLanguage"
    class="w-28"
  >
    <template #prefix>
      <img :src="language === 'zh' ? ChineseImg : EnglishImg" style="width: 20px" />
    </template>
    <el-option value="zh">
      <div style="display: flex; align-items: center">
        <img :src="ChineseImg" style="width: 20px; margin-right: 8px" />
        zh
      </div>
    </el-option>
    <el-option value="en">
      <div style="display: flex; align-items: center">
        <img :src="EnglishImg" style="width: 20px; margin-right: 8px" />
        en
      </div>
    </el-option>
  </el-select>
</template>

<script setup>
import { useI18n } from 'vue-i18n'
import { useAppStore } from '@/stores/modules/app'
import { ElMessage } from 'element-plus'
import { computed, watch } from 'vue'
import ChineseImg from '@/assets/images/chinese.png'
import EnglishImg from '@/assets/images/english.png'

defineProps({
  effect: {
    type: String,
    default: 'light',
    validator: function (value) {
      // 这个值必须匹配下列字符串中的一个
      return ['dark', 'light'].indexOf(value) !== -1
    }
  }
})

const appStore = useAppStore()
const language = computed(() => {
  return appStore.language
})

// 切换语言的方法
const i18n = useI18n()
const handleSetLanguage = (lang) => {
  appStore.setLanguage(lang)
  i18n.locale.value = lang
  ElMessage.success(i18n.t('msg.toast.switchLangSuccess'))
}
</script>
